<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel"
      >
        <template slot="menuLeft">
          <el-button
            v-if="permissions.workorder_hdviewapiitem_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel"
            >导出
          </el-button>
        </template>

        <template slot-scope="{ row, index }" slot="menuBtn">
          <el-dropdown-item
            v-if="row.$cellEdit"
            type="text"
            size="small"
            style="display: flex; justify-content: space-between"
          >
            <div @click="handleCancel(row, index)">取消</div>
            <div
              style="margin-left: 10px"
              @click="
                handleUpdate(
                  row,
                  () => {},
                  () => {}
                )
              "
            >
              保存
            </div>
          </el-dropdown-item>
          <el-dropdown-item
            v-else
            type="text"
            size="small"
            :icon="'el-icon-edit'"
            @click.native="editRow(row, index)"
          >
            编辑
          </el-dropdown-item>
          <el-dropdown-item
            divided
            icon="el-icon-delete"
            size="small"
            @click.native="handleDel(row, index)"
            >删除</el-dropdown-item
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  addObj,
  putObj,
  delObj,
} from "@/api/workorder/hdviewapiitem";
import { tableOption } from "@/const/crud/workorder/hdviewapiitem";
import { mapGetters } from "vuex";
import cascaderMultiWorkshopsLineSelect from "@/views/workorder/workordercps/cascader-multi-workshops-line-select.vue";

export default {
  name: "hdviewapiitem",
  components: { cascaderMultiWorkshopsLineSelect },
  data() {
    return {
      searchForm: {},
      tableData: [],
      workingHour: null, //标准工时
      originalRows: {}, // 用于存储原始数据的备份
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.workorder_hdviewapiitem_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.workorder_hdviewapiitem_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.workorder_hdviewapiitem_edit,
          false
        ),
      };
    },
  },
  methods: {
    // 列表查询
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
          },
          params,
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    // 删除
    handleDel: function (row, index) {
      this.$confirm("是否确认删除品号为" + row.itemCode, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.itemCode);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    // 更新
    handleUpdate: function (row, index, done, loading) {
      console.log(this.workingHour);
      console.log(1, row);
      putObj(row)
        .then((data) => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    // 保存
    handleSave: function (row, done, loading) {
      
      addObj(row)
        .then((data) => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current;
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page);
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile(
        "/workorder/hdviewapiitem/export",
        this.searchForm,
        "hdviewapiitem.xlsx"
      );
    },
    workingHourChange(row) {
      if (this.validatenull(row)) {
        this.$set(this.$refs.crud.tableForm, "workingHour", this.workingHour);
      } else {
        row.workingHour = this.workingHour;
      }
      // 添加数值范围校验
      if (this.workingHour < 0) {
        this.$message.warning("工时不能为负数");
        this.workingHour = 0;
      }
    },
    editRow(row, index) {
      // 深拷贝原始数据
      this.$set(this.originalRows, row.id, JSON.parse(JSON.stringify(row)));
      // 进入编辑模式
      this.$refs.crud.rowCell(row);
    },
    handleCancel(row, index) {
      this.$refs.crud.rowCancel(row, index);
      this.getList(this.page);
      // 从备份中恢复原始数据
      // const original = this.originalRows[row.id]
      // Object.keys(original).forEach(key => {
      //   row[key] = original[key]
      // })
      // // 退出编辑模式
      // this.$refs.crud.rowCancel(row, index)
      // // 清除备份（可选）
      // delete this.originalRows[row.id]
    },
  },
};
</script>
